import Notify from '../../vant/notify/notify';
//引入工具类
const util = require('../../utils/util');
// 加载配置文件
const config = require('../../utils/config');

const app = getApp()

Page({
    data: {
        loading: false,
        visible: false,
        email: '',
        phone: '',
        username: '',
        password: '',
        repassword: ''
    },
    goLogin() {
        wx.navigateBack();
    },
    bindInput(e) {
        let type = e.currentTarget.dataset.type
        this.setData({
            [type]: e.detail
        })
    },
    tipShow(msg) {
        Notify({
            text: msg,
            duration: 1000,
            selector: '#custom-selector'
        });
    },
    handleOk() {
        this.setData({
            visible: false
        });
        this.goLogin();
    },
    handleClick() {
        let _this = this;
        if (_this.data.email == '') {
            _this.tipShow('请输入邮箱')
            return;
        }
        if (_this.data.phone == '') {
            _this.tipShow('请输入手机号码')
            return;
        }
        if (_this.data.username == '') {
            _this.tipShow('请输入用户名')
            return;
        }
        if (_this.data.password == '') {
            _this.tipShow('请输入密码')
            return;
        }
        if (_this.data.repassword == '') {
            _this.tipShow('请输入确认密码')
            return;
        }

        if (!util.validatorEmail(_this.data.email)) {
            _this.tipShow('邮箱格式不正确')
            return;
        }
        if (!util.validatorPhone(_this.data.phone)) {
            _this.tipShow('手机号码格式不正确')
            return;
        }
        if (!util.validatorPassword(_this.data.password)) {
            _this.tipShow('密码为6-20位数字、字母')
            return;
        }
        if (_this.data.password != _this.data.repassword) {
            _this.tipShow('两次密码不一致')
            return;
        }

        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '注册中...'
        })

        app.ajax({
            url: `${config.httpUrl}log/regist`,
            method: 'post',
            data: {
                email: _this.data.email,
                phone: _this.data.phone,
                username: _this.data.username,
                password: _this.data.password
            },
            callback: function(error, data) {
                if (+data.data.code === 0) {
                    _this.setData({
                        visible: true
                    });
                } else {
                    _this.tipShow(data.data.msg)
                }
                wx.hideLoading();
                _this.setData({
                    loading: false
                })
            }
        })
    }
})